home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!news
- From: watzka@stat.uni-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: Forward Struct Reference
- Date: 21 Mar 1996 16:39:48 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4is0ok$7ju@sparcserver.lrz-muenchen.de>
- References: <4iqu1p$edq@newsbf02.news.aol.com>
- NNTP-Posting-Host: sun2.lrz-muenchen.de
-
- shinjinrui@aol.com (ShinJinRui) writes:
-
- >What is the best way to declare a typdef for both a struct and a contained
- >function that operates on it? I want to have a structure that contains a
- >pointer to a function that operates on the structure.
-
- >example:
- >/*
- >* declare typedef to a function "FuncType" which operates on a
- >pointer
- >* to a struct of type "structType" (typedef of structType follows
- >below).
- >*/
- >typedef void FuncType(*structType);
-
- Even if the definition of "structType" precedes this prototype, it
- is still wrong. You wanted to write
-
- typedef void FuncType(structType *);
-
- BTW, in most cases defining a function type is _not_ what you want.
-
- Since "structType" has not been introduced at this point, and you
- obviously want to avoid that, you can use an incomplete type. This
- can be done in at least two different ways.
-
- 1.) You can use the struct "tag". A struct that has not been
- introduced is an incomplete type.
-
- typedef void FuncType(struct structType_tag *);
-
- 2.) You can use the incomplete type in a typedef statement.
-
- typedef struct structType_tag structType;
-
- typedef void FuncType(structType *);
-
- >/*
- >* Declare the structure's type
- >*/
- >typedef struct structType_tag {
- > int a;
- > int b;
- > void (*FuncType)(*structType);
-
- What is this supposed to do? If you want the struct to have a member
- that is a pointer to a function that takes a struct as an argument,
- there are some ways to do this:
-
- 1.) Straightforward:
-
- typedef struct structType_tag {
- int a;
- int b;
- void (* f)(struct structType_tag *);
- } structType;
-
- 2.) Using you typedef statements from above:
-
- typedef struct structType_tag structType;
- typedef void FuncType(structType *);
-
- struct structType_tag
- {
- int a;
- int b;
- FuncType *f;
- };
-
- 3.) Using a function pointer type (This is a common approach)
-
- typedef void (* FuncPtr)(struct structType_tag *);
- typedef struct structType_tag
- {
- int a;
- int b;
- FuncPtr f;
- } structType;
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
- | ua302aa@sunmail.lrz-muenchen.de
-